const {BrowserWindow, app, Menu, Tray, Notification} = require('electron')
const path = require('path')
let tray = null,
    showKey = true,
    notification = null,
    window = null,
    icon_path = path.resolve(__dirname, app.isPackaged ? '../../../public/logos/favicon.ico' : '../../public/logos/favicon.ico');


const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
    app.quit()
    process.exit(1)
} else {
    app.on('second-instance', (event, commandLine, workingDirectory) => {
        // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
        if (window) {
            if (window.isMinimized()) window.restore()
            window.focus()
            window.show()
        }
    });
}

app.setAppUserModelId('游戏性能辅助监控')

function createMainWindow() {
    const window = new BrowserWindow({
        x: 1500,
        y: 100,
        width: 150,
        height: 100,
        minWidth: 150,
        minHeight: 100,
        maxWidth: 150,
        maxHeight: 100,
        hasShadow: true,
        movable: true,//是否可以移动
        resizable: true,//是否可以调节窗口大小
        minimizable: true, //是否可以最大化
        maximizable: true, //是否可以最小化
        transparent: true,//设置窗口为透明
        frame: false,//无边框窗口
        alwaysOnTop: true,//置顶窗口
        skipTaskbar: true,//任务栏显示窗口
        title: '游戏性能辅助监控',
        webPreferences: {
            //开启node支持
            nodeIntegration: true,
            contextIsolation: false,
            webviewTag: true, //开启webview支持
            preload: path.resolve(__dirname, '../preload/preload.js') //预加载脚本
        }
    })
    // window.webContents.openDevTools()
    // window.setFullScreen(true);
    window.setAlwaysOnTop(true, "pop-up-menu");//设置置顶和置顶级别
    return window
}

function createNotification(option = {}) {
    return new Notification({
        ...option,
        title: option.title,
        body: option.body,
        silent: !option.silent,//是否发出声音
        icon: option.icon
    })
}


app.on('ready', function () {
    notification = createNotification({
        title: '温馨提示',
        body: '左键点击通知栏图标可以切换监控在穿透模式和非穿透模式间切换',
        silent: true,
        icon: icon_path
    })
    notification.show()
    window = createMainWindow()
    window.loadURL(app.isPackaged //判断是否打包
        ? path.resolve(__dirname, '../render/index.html')
        : `http://localhost:${process.env.VITE_PORT}#/`)
})

app.whenReady().then(() => {
    tray = new Tray(icon_path)
    const contextMenu = Menu.buildFromTemplate([
        {
            label: '修改弹窗标题',
            role: 'minimize',
            type: 'normal',
            click() {
                notification.title = 'xxxxxxxxxxxx'
                notification.show()
            }
        },
        {
            label: '最小化',
            role: 'minimize',
            type: 'normal',
            click() {
                window.hide();
            }
        },
        {
            label: '还原',
            role: 'minimize',
            type: 'normal',
            click() {
                window.show();
            }
        },
    ]);
    tray.setToolTip('游戏性能辅助监控')
    tray.setContextMenu(contextMenu);
    tray.on('click', e => {
        console.log('左键')
        console.log(window.isMinimized()) //窗口是否最小化
        console.log(window.isVisible()) //窗口是否可见
        if (window.isMinimized() || !window.isVisible()) {
            window.show()
        }
        window.setIgnoreMouseEvents(showKey)
        showKey = !showKey
        // console.log(e)
    })
    tray.on('right-click', e => {
        console.log('右键')
        window.setIgnoreMouseEvents(false)
        // console.log(e)
    })
    tray.on('double-click', e => {
        console.log('双击')
        // console.log(e)
    })
    tray.on('balloon-show', e => {
        console.log('托盘气泡显示')
        console.log(e)
    })
    tray.on('balloon-click', e => {
        console.log('托盘气泡点击')
        console.log(e)
    })
    tray.on('balloon-closed', e => {
        console.log('托盘气泡关闭')
        console.log(e)
    })
})


